package com.blue.excel.demo;

import com.blue.excel.model.ColumnField;
import com.blue.excel.model.OverrideTitle;
import com.blue.excel.util.ExcelTransformer;
import com.blue.excel.util.ValueSetInterceptor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by ChenYaHui on 2017/8/1.
 */
public class TestMain {
    public static void main(String[] args) {
        CustomEntity entity = new CustomEntity();
        entity.name = "A";
        entity.age = 12;
        entity.createDate = new Date();
        entity.child = new Child();
        entity.child.phone = "156";
        entity.child.address = "一环";
        entity.child.weight = 50;

        entity.child1 = new Child();
        entity.child1.phone = "159";
        entity.child1.address = "三环";
        entity.child1.weight = 90;

        entity.extendMap.put("爱好","书法");
        entity.extendMap.put("英文水平","六级");
        entity.extendMapDouble.put("身高",170D);
        entity.extendMapDouble.put("腰围",30D);
        entity.splitChild.add(new Child("187","20.658%",190));
        entity.splitChild.add(new Child("189","中山路",195));

        entity.infoSet.add(new OtherChild("1月","国税局","营业税",234921.12));
        entity.infoSet.add(new OtherChild("2月","国税局","车辆购置税",23123.21));
//        entity.bgColor = 1;

        CustomEntity entity1 = new CustomEntity();
        entity1.name = "  B";
        entity1.age = 20;
        entity1.child = new Child();
        entity1.child.phone = "159";
        entity1.child.address = "二环";
        entity1.child.weight = 70;

        entity1.extendMap.put("英文水平","四级");
        entity1.extendMap.put("体育","擅长");

        entity1.extendMapDouble.put("身高",150D);
        entity1.extendMapDouble.put("腰围",21D);
        entity1.splitChild.add(new Child("133", "和平路", 160));

        CustomEntity entity2 = new CustomEntity();

        entity2.name = "  B";
        entity2.age = 20;
        entity2.child = new Child();
        entity2.child.phone = "159";
        entity2.child.address = "二环";
        entity2.child.weight = 70;

        entity2.extendMap.put("英文水平","四级");
        entity2.extendMap.put("体育","普通");

        entity2.extendMapDouble.put("身高",150D);
        entity2.extendMapDouble.put("腰围",21D);
        entity2.splitChild.add(new Child("133", "和平路", 160));
        entity2.bgColor = 1;

        entity.child = null;
        entity1.child = null;
        entity2.child = null;
        List<CustomEntity> list = new ArrayList<>();
        list.add(entity);
        list.add(entity1);
        list.add(entity2);


        ExcelTransformer tool = new ExcelTransformer();

        ValueSetInterceptor div = new ValueSetInterceptor() {
            @Override
            public Object change(Object obj) {
                if(obj instanceof Integer){
                    return ((Integer)obj) * 10;
                }
                else if(obj instanceof Double){
                    return ((Double)obj) / 10;
                }
                return null;
            }
        };

        ColumnField.registeredInterceptors.put("div",div);

//        tool.setColorSeparate(false);
        List<OverrideTitle> overrideTitles = new ArrayList<>();
        OverrideTitle overrideTitle = new OverrideTitle("名字","name");
        overrideTitles.add(overrideTitle);
        OverrideTitle overrideTitle1 = new OverrideTitle("家庭住址","child.address");
        OverrideTitle overrideTitle2 = new OverrideTitle("家庭住址1","child1.address");
        overrideTitles.add(overrideTitle1);
        overrideTitles.add(overrideTitle2);

        List<String> resortFieldNames = new ArrayList<>();
        resortFieldNames.add("name");
        resortFieldNames.add("age");
        resortFieldNames.add("createDate");
        resortFieldNames.add("extendMapDouble");
        resortFieldNames.add("child");

        tool.setNextSheetBigTitle("新的标题  测试");

        tool.makeSheet(CustomEntity.class,null, overrideTitles, resortFieldNames);



        tool.parseToSheet(list,CustomEntity.class);
        tool.makeSheet(CustomEntity.class);





        Workbook workbook = tool.getWorkbook();


//        new Tool(workbook.getSheetAt(0)).deleteColumns(2).autoColumnWidth(2,3,4);



        InputStream in = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(new File("D:/g.xls"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(outputStream);
            in = new ByteArrayInputStream(out.toByteArray());
        } catch (IOException ex) {
            Logger.getLogger(TestMain.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
